/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initExecuteCommand, runHvigor } from './common/common';

describe('build app signing test', () => {
  test.concurrent('test stage app build app not signing', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'assembleApp'
      });
      await testSignFalse(projectRoot, commands);
    }, { sign: false });
  });

  test.concurrent('test stage app build app signing', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'assembleApp'
      });
      await testSignTrue(projectRoot, commands);
    }, { sign: true });
  });
});

export async function testSignFalse(projectRoot: string, commands: string[]) {
  const result = runHvigor(projectRoot, commands);

  // Api8 涉及SignShell任务时不支持增量
  if (result.stdout.includes('SignShell')) {
    expect(result.stdout).toContain('Finished ::SignApp');
    return;
  }

  expect(result.stderr)
    .toContain('Will skip sign \'app\'. No signingConfigs profile is configured in current project.');
  expect(result.stdout).toContain('Finished :entry:default@SignHap');
  expect(result.stdout).toContain('Finished ::SignApp');
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
  const result2 = runHvigor(projectRoot, commands);
  expect(result2.stderr)
    .toContain('Will skip sign \'app\'. No signingConfigs profile is configured in current project');
  expect(result2.stdout).toContain('Finished ::SignApp');
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  const output = path.resolve(projectRoot, 'build/outputs');
  await fse.removeSync(output);
  const result3 = runHvigor(projectRoot, commands);
  expect(result3.stderr
  ).toContain('Will skip sign \'app\'. No signingConfigs profile is configured in current project');
  expect(result3.stdout).toContain('Finished ::SignApp');
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
}

export async function testSignTrue(projectRoot: string, commands: string[]) {
  const result = runHvigor(projectRoot, commands);

  // Api8 涉及SignShell任务时不支持增量
  if (result.stdout.includes('SignShell')) {
    expect(result.stdout).toContain('Finished ::SignApp');
    return;
  }
  expect(result.stdout).toContain('Finished ::SignApp');
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
  const result2 = runHvigor(projectRoot, commands);
  expect(result2.stdout).toContain('UP-TO-DATE ::SignApp');
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  const output = path.resolve(projectRoot, 'build/outputs');
  await fse.removeSync(output);
  const result3 = runHvigor(projectRoot, commands);
  expect(result3.stdout).toContain('Finished ::SignApp');
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
}
